package or.exa.cangguan.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.common.Result;
import or.exa.cangguan.entity.dto.GoodsDto;
import or.exa.cangguan.entity.vo.OutOperationVo;
import or.exa.cangguan.service.OutOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "出库作业单")
public class OutOperationController {

    @Autowired
    private OutOperationService outOperationService;

    /**
     * 查询出库作业单
     */
    @ApiOperation(value = "查询出库作业单")
    @PostMapping("/outOperation/search")
    public Result searchOutOperation(@RequestBody OutOperationVo outOperationVo) {
        Page page = outOperationService.searchOutOperation(outOperationVo);
        return Result.ok(page);
    }

    /**
     * 添加出库作业单
     */
    @ApiOperation(value = "添加出库作业单")
    @PostMapping("/outOperation/add")
    public Result addOutOperation(@RequestBody OutOperationVo outOperationVo) {
        Integer i = outOperationService.addOutOperation(outOperationVo);
        return i>0?Result.ok("添加成功"):Result.ok("添加失败");
    }

    /**
     * 更新出库作业单
     */
    @ApiOperation(value = "更新出库作业单")
    @PostMapping("/outOperation/update")
    public Result updateOutOperation(@RequestBody OutOperationVo outOperationVo) {
        Integer i = outOperationService.updateOutOperation(outOperationVo);
        return i>0?Result.ok("更新成功"):Result.ok("更新失败");
    }

    /**
     * 查询货物所放位置
     */
    @ApiOperation(value = "查询货物所放位置")
    @PostMapping("/outOperation/searchGoods/{id}")
    public Result searchGoods(@PathVariable Integer id) {
        List<GoodsDto> goodsDtos = outOperationService.searchGoods(id);
        return Result.ok(goodsDtos);
    }

    /**
     * 更新状态
     */
    @ApiOperation(value = "更新状态")
    @PostMapping("/outOperation/updateStatus")
    public Result updateStatus(@RequestBody OutOperationVo outOperationVo) {
//        outOperationService.reduceGoods(outOperationVo.getOutId(),outOperationVo.getStatus());
        Integer i = outOperationService.updateStatus(outOperationVo.getOutId(), outOperationVo.getStatus());
        if (i==0){
            return Result.ok("商品未分配");
        } else if (i==-1){
            return Result.ok("未开始");
        } else  {
            return Result.ok("更新状态成功");
        }

    }
}
